package ai.xiaodao.config;

import ai.xiaodao.tool.ConfigUtil;
import org.apache.log4j.Logger;

import java.util.Properties;

import static java.lang.System.exit;

/**
 * 邮件配置信息
 */
public class MailConfig {

    static Logger log = Logger.getLogger(MailConfig.class.getName());
    static String name;
    static String server;
    static Integer port;
    static String passwd;
    static String receiveAddr;
    static boolean enableAuth;
    static boolean enableTTL;


    public static void init(String conf) {
        boolean initResult = false;
        //check the input config file path
        if (null != conf && conf.length() > 0) {
            Properties prop = ConfigUtil.readProps(conf);
            if (!paramsCheck(prop)) {
                log.error(conf + "config error please check");
                exit(1);
            }
            name = prop.getProperty(Const.MAILE_NAME_KEY);
            passwd = prop.getProperty(Const.MAILE_PASS_KEY);
            port = Integer.valueOf(prop.getProperty(Const.MAILE_PORT_KEY));
            receiveAddr = prop.getProperty(Const.MAILE_RECEIVE_ADDR_KEY);
            enableAuth = Boolean.valueOf(prop.getProperty(Const.MAILE_ENABLE_AUTH_KEY));
            enableTTL = Boolean.valueOf(prop.getProperty(Const.MAILE_ENABLE_TTL_KEY));
            server = prop.getProperty(Const.MAILE_SERVER_KEY);

        } else {
            log.error("The mail config path in correct please check");
            exit(1);
        }

    }


    /**
     * 配置项检查，将所有的配置项都检查一遍并给出错误信息。避免在配置有多项错误的时候，每次启动只检查一项
     *
     * @param prop 配置文件内容
     * @return true if all config legal false if not
     */
    static private boolean paramsCheck(Properties prop) {

        //todo valid  input
        return true;
    }


}
